import { JsEnvExample7, System, UnityEngine } from "csharp";
import { $promise, $typeof } from "puerts";

let cube1 = UnityEngine.GameObject.Find("Cube1");
let cube2 = UnityEngine.GameObject.Find("Cube2");
let btn = UnityEngine.GameObject.Find("Button1");
let btnComponent = btn.GetComponent($typeof(UnityEngine.UI.Button)) as UnityEngine.UI.Button;

btnComponent.onClick.AddListener(
    async ()=>{
        // await $promise(JsEnvExample7.RotateFroSeconds(cube1,1));
        // await WaitTimeForSeconds(1.5);
        // await $promise(JsEnvExample7.RotateFroSeconds(cube2,2));
        await RotateForSeconds(cube1,1);
        await WaitTimeForSeconds(Math.random() + 1);
        await RotateForSeconds(cube2,1);
    }
)

async function WaitTimeForSeconds(sec:number) {
    console.log("wait time:" + sec );
    
    return new Promise<void>((resolve,reject)=>{
        setTimeout(() => {
            resolve();
        }, sec * 1000);
    });
}
//System.Threading.Tasks.Task.Yield
async function RotateForSeconds(go:UnityEngine.GameObject,d:number) {
    let end = UnityEngine.Time.time + d;
    while(UnityEngine.Time.time < end){
        go.transform.Rotate(UnityEngine.Vector3.op_Multiply(new UnityEngine.Vector3(1,1),UnityEngine.Time.deltaTime * 150));
        await $promise(<any>System.Threading.Tasks.Task.Yield());
    }
}
